package zero.security.controller;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import com.jfinal.kit.PathKit;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.template.Engine;

import zero.common.controller.BaseAdminController;
import zero.common.utils.CodeUtils;

public class GeneratorController extends BaseAdminController {

	@Override
	public void index() {

	}

	public void pageJson() {
		// 查数据库表
		List<Record> records = Db.use().find(
				"select table_name tableName, engine, table_comment tableComment, create_time createTime from information_schema.tables where table_schema = (select database()) order by create_time desc");
		setAttr("data", records);
		setAttr("code", 0);
		renderJson();
	}

	@Override
	public void add() {

	}

	@Override
	public void save() {

	}

	@Override
	public void edit() {

	}

	@Override
	public void update() {

	}

	@Override
	public void delete() {

	}

	/**
	 * 导出代码生成文件
	 */
	public static final String TEMPLATE_CONTROLLER = PathKit.getWebRootPath()+"/static/generatorTemplate/Controller.txt";
	public static final String TEMPLATE_MODEL = PathKit.getWebRootPath()+"/static/generatorTemplate/model.txt";
	public static final String TEMPLATE_HTML_INDEX = PathKit.getWebRootPath()+"/static/generatorTemplate/index.txt";
	public static final String TEMPLATE_HTML_ADD = PathKit.getWebRootPath()+"/static/generatorTemplate/add.txt";
	public static final String TEMPLATE_HTML_EDIT = PathKit.getWebRootPath()+"/static/generatorTemplate/edit.txt";
	public void export() {
		try {

		// 数据表名
		String tableName = getPara("tableName", "xc_common_demo");
		// 过滤头
		String tableHeadFilter = getPara("tableHeadFilter", "xc_");
		// 类名
		String modelClassName = CodeUtils.camelName(tableName, tableHeadFilter);
		// 小写变量类名
		String modelClassname = StringUtils.uncapitalize(modelClassName);
		// Controller名称
		String controllerClassName = modelClassName + "Controller";
		// 文件存放位置
		String filePath = "D:\\generator\\"+tableName+"\\";

		/* ***** Controller/model 相关 ****** */
		Map<String, Object> controllerDatas = new HashMap<>();
		controllerDatas.put("tableName", tableName);
		controllerDatas.put("modelClassName", modelClassName);
		controllerDatas.put("modelClassname", modelClassname);
		controllerDatas.put("controllerClassName", controllerClassName);

		String controllerString = Engine.use().getTemplate(TEMPLATE_CONTROLLER).renderToString(controllerDatas);
		String modelString = Engine.use().getTemplate(TEMPLATE_MODEL).renderToString(controllerDatas);
		System.out.println(controllerString);
		System.out.println(modelString);

		/* ***** html 相关 ****** */

		// 数据表属性字段
		// dataType:mediumint
		// extra:auto_increment
		// columnComment:
		// columnKey:PRI
		// columnName:id
		List<Record> tableProperty = Db.use().find(
				"select column_name columnName, data_type dataType, column_comment columnComment, column_key columnKey, extra	 from information_schema.columns where table_name = ? and table_schema = (select database()) order by ordinal_position",
				tableName);

		// index页面模板
		Map<String, Object> htmlDatas = new HashMap<>();
		htmlDatas.put("modelClassName", modelClassName);
		htmlDatas.put("shiroView", "#shiroHasPermission('security:menu:add')"); // 查看权限
		htmlDatas.put("shiroAdd", "#shiroHasPermission('security:menu:add')"); // 添加权限
		htmlDatas.put("shiroEdit", "#shiroHasPermission('security:menu:add')"); // 编辑权限
		htmlDatas.put("shiroDelete", "#shiroHasPermission('security:menu:add')"); // 删除权限
		htmlDatas.put("toolUrl", "/xxx/xxx"); // 增删改查基础地址
		htmlDatas.put("tableinfos", tableProperty);
		htmlDatas.put("modelClassname", modelClassname);

		String htmlIndexString = Engine.use().getTemplate(TEMPLATE_HTML_INDEX).renderToString(htmlDatas);
		String htmlAddString = Engine.use().getTemplate(TEMPLATE_HTML_ADD).renderToString(htmlDatas);
		String htmleditString = Engine.use().getTemplate(TEMPLATE_HTML_EDIT).renderToString(htmlDatas);

		System.out.println(htmlIndexString);
		System.out.println(htmlAddString);
		System.out.println(htmleditString);

		// 开始转文件
		CodeUtils.string2File(controllerString, filePath + modelClassName + "Controller.java");
		CodeUtils.string2File(modelString, filePath + modelClassName + ".java");
		CodeUtils.string2File(htmlIndexString, filePath + "index.html");
		CodeUtils.string2File(htmlAddString, filePath + "add.html");
		CodeUtils.string2File(htmleditString, filePath + "edit.html");
		
	} catch (Exception e) {
		e.printStackTrace();
	}
		renderJson();
	}

}
